package main

import (
	"log"
	"net/http"
	"net/rpc"

	"gitee.com/mayxiangfan/go-study/rpc/models"
)

type UserService struct{}

// rpc 格式要求必须为 func (t *struct) xxx(request,*response) error
func (u *UserService) GetUser(request models.UserRequest, response *models.UserResponse) error {
	users := []string{"张三", "李四"}
	log.Println("request id:", request.Id)
	response = &models.UserResponse{}
	if request.Id == 0 {
		response.Name = users
		response.Age = request.Id + 1
	} else {
		response.Name = users
		response.Age = request.Id + 1
	}
	log.Println(response)
	return nil
}

func main() {
	//1.实例UserService
	us := new(UserService) // == &UserService{}
	//2.注册服务
	rpc.Register(us)
	//3.绑定到HandleHTTP
	rpc.HandleHTTP()
	//监听http服务
	log.Fatal(http.ListenAndServe(":8082", nil))
}
